package com.xiahui.day08.Test1;

import java.util.Arrays;

/**
 * toString(数组名)：将数组的元素转换为字符串
 * copyOf(数组名，新长度)：复制数组
 * sort(数组名)：将数组按照升序排序
 * fill(数组名，填充元素)：将数组按照指定元素进行填充
 * binarySearch(数组名，元素)：使用二分查找法查找某个元素在数组中的下标
 * (注意：二分查找的那个数组必须先排序)
 */
public class TestArrays {
    public static void main(String[] args) {
        int[] nums = {52, 65, 9, 36, 78, 10};

        Arrays.sort(nums);
        System.out.println(Arrays.toString(nums));
        System.out.println("-----------------------------------------");

        int index = Arrays.binarySearch(nums, 10);

        System.out.println("index = " + index);
        System.out.println("---------------------------------------");
        int[] newArr = new int[nums.length];

        for (int i = 0, j = nums.length - 1; i < nums.length; i++, j--) {
            newArr[i] = nums[j];
        }
        System.out.println(Arrays.toString(newArr));


        System.out.println("--------------------------------------");

        Arrays.fill(nums, 666);

        System.out.println(Arrays.toString(nums));
//遍历二维数组
        int[][] matrix = {
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12}
        };

        for (int i = 0; i < matrix.length; i++) {  // 遍历每一行
            for (int j = 0; j < matrix[i].length; j++) {  // 遍历当前行的每一列
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();  // 换行
        }


    }
}


